package com.ruoyi.net.service.impl;

import java.util.Collection;
import java.util.List;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.net.domain.NetGroup;
import com.ruoyi.net.service.INetElementPortService;
import com.ruoyi.net.service.INetGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.net.mapper.NetElementMapper;
import com.ruoyi.net.domain.NetElement;
import com.ruoyi.net.service.INetElementService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;

/**
 * 网元设备Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-27
 */
@Service
public class NetElementServiceImpl implements INetElementService 
{
    @Autowired
    private NetElementMapper netElementMapper;

//    @Autowired
//    private NetGroupMapper netGroupMapper;

    @Autowired
    private INetGroupService netGroupService;

    @Autowired
    private INetElementPortService netElementPortService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 项目启动时，初始化网元信息到缓存
     */
    @PostConstruct
    public void init()
    {
        loadingNetElementCache();
    }

    /**
     * 清空缓存数据
     */
    @Override
    public void clearNetElementCache()
    {
        Collection<String> keys = redisCache.keys(CacheConstants.NET_ELEMENT_KEY + "*");
        redisCache.deleteObject(keys);
    }

    /**
     * 重置缓存数据
     */
    @Override
    public void resetConfigCache()
    {
        clearNetElementCache();
        loadingNetElementCache();
    }

    @Override
    public void loadingNetElementCache() {
        List<NetElement> elements = netElementMapper.selectNetElementList(new NetElement());
        for (NetElement element : elements) {
            redisCache.setCacheObject(getCacheKey(element.getId()), element);
        }
    }

    @Override
    public NetElement getCacheObject(String id){
        return (NetElement) redisCache.getCacheObject(getCacheKey(id));
    }

    /**
     * 查询网元设备
     * 
     * @param id 网元设备主键
     * @return 网元设备
     */
    @Override
    public NetElement selectNetElementById(String id)
    {
        return netElementMapper.selectNetElementById(id);
    }

    /**
     * 查询网元设备列表
     * 
     * @param netElement 网元设备
     * @return 网元设备
     */
    @Override
    public List<NetElement> selectNetElementList(NetElement netElement)
    {
        return netElementMapper.selectNetElementList(netElement);
    }

    /**
     * 新增网元设备
     * 
     * @param netElement 网元设备
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNetElement(NetElement netElement) {
        int repeatSns = 0;
        if (StringUtils.isNotEmpty(netElement.getSn())) {
            repeatSns = netElementMapper.checkNetElementSn(netElement.getSn());
        }
        if (repeatSns > 0) {
            throw new RuntimeException("序列号sn重复");
        }
        String uuid = IdUtils.simpleUUID();
        if (StringUtils.isEmpty(netElement.getId())) {
            netElement.setId(uuid);
        }
        netElement.setCreateTime(DateUtils.getNowDate());
        netElement.setCreateBy(SecurityUtils.getUsername());

        NetGroup netGroup = new NetGroup();
        netGroup.setId(netElement.getId());
        netGroup.setParentId(netElement.getGroupId());
        netGroup.setGroupName(netElement.getElementName());
        netGroup.setGroupType("2"); // 网元
        netGroup.setDisplayStatus(netElement.getDisplayStatus());
//        netGroup.setCreateTime(DateUtils.getNowDate());
        netGroupService.insertNetGroup(netGroup);
        int row = netElementMapper.insertNetElement(netElement);
        if (row > 0) {
            redisCache.setCacheObject(getCacheKey(netElement.getId()), netElement);
        }
        return row;
    }

    /**
     * 修改网元设备
     * 
     * @param netElement 网元设备
     * @return 结果
     */
    @Override
    public int updateNetElement(NetElement netElement)
    {
        /*NetElement oriNetElement = netElementMapper.selectNetElementById(netElement.getId());
        if (oriNetElement != null
                && !oriNetElement.getElementName().equals(netElement.getElementName())
                && !oriNetElement.getDisplayStatus().equals(netElement.getDisplayStatus())
        ) {*/
            // 更新 逻辑域中网元
            NetGroup netGroup = new NetGroup();
            netGroup.setId(netElement.getId());
            netGroup.setGroupName(netElement.getElementName());
            netGroup.setDisplayStatus(netElement.getDisplayStatus());
            netGroupService.updateNetGroup(netGroup);
        /*}*/
//        netElement.setUpdateBy(SecurityUtils.getUsername());
        netElement.setUpdateTime(DateUtils.getNowDate());
        int row = netElementMapper.updateNetElement(netElement);
        if (row > 0) {
            redisCache.setCacheObject(getCacheKey(netElement.getId()), netElement);
        }
        return row;
    }

    /**
     * 定时任务ping，更新设备状态
     * @param netElement
     * @return
     */
    @Override
    public int updateElementStatus(NetElement netElement) {
        // 更新 逻辑域中网元
        NetGroup netGroup = new NetGroup();
        netGroup.setId(netElement.getId());
        netGroup.setGroupName(netElement.getElementName());
        netGroup.setDisplayStatus(netElement.getDisplayStatus());
        netGroupService.updateNetGroup(netGroup);

        // 修改网元显示状态
        netElement.setUpdateBy("admin");
        netElement.setUpdateTime(DateUtils.getNowDate());
        int row = netElementMapper.updateNetElementDisplayStatus(netElement);
        if (row > 0) {
            redisCache.setCacheObject(getCacheKey(netElement.getId()), netElement);
        }
        return row;
    }

    /**
     * 批量删除网元设备
     * 
     * @param ids 需要删除的网元设备主键
     * @return 结果
     */
    @Override
    public int deleteNetElementByIds(String[] ids) {
        if (!netElementPortService.checkPortBindStatusByElementId(ids)) {
            throw new RuntimeException("网元设备存在已绑定端口，无法删除");
        }
        int row = netElementMapper.deleteNetElementByIds(ids);
        if (row > 0) {
            for (String id : ids) {
                redisCache.deleteObject(getCacheKey(id));
            }
        }
        return row;
    }

    /**
     * 删除网元设备信息
     * 
     * @param id 网元设备主键
     * @return 结果
     */
    @Override
    public int deleteNetElementById(String id)
    {
        int row = netElementMapper.deleteNetElementById(id);
        if (row > 0) {
            redisCache.deleteObject(getCacheKey(id));
        }
        return row;
    }

    /**
     * 设置cache key
     *
     * @param key 参数键
     * @return 缓存键key
     */
    private String getCacheKey(String key)
    {
        return CacheConstants.NET_ELEMENT_KEY + key;
    }
}
